import cv2
import numpy as np

from _00tools import cv_show
"""
@Project: pythonPro1
@Name: _14fourier.py
@Author: linxin_liu
@Date: 2022/10/16 18:42
傅里叶
"""
img_ori = cv2.imread('D:/tools/image_operation/bing.png', 0)
img_float32 = np.float32(img_ori)  # 数据转换
dft = cv2.dft(img_float32, flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))

rows, cols = img_ori.shape
crow, ccol = int(rows/2), int(cols/2)

mask = np.zeros((rows, cols, 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 1

fshift = dft_shift*mask
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])

cv2.imshow('a', img_back)  # 创建一个窗口，命名image，并展示窗口
cv2.waitKey(0)  # 窗口显示时间，参数为0表示按任意键销毁
cv2.destroyAllWindows()  # 销毁所有窗口
